//
//  TabBarController.m
//  OrderSeat
//
//  Created by wangwei on 13-10-4.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "TabBarController.h"
#import "HomeViewController.h"
#import "NearbyViewController.h"
#import "MeViewController.h"
#import "PhotoViewController.h"
#import "PhotoViewController.h"
#import "MoreViewController.h"
#import "LoggedViewController.h"
#import "AppSetting.h"

@implementation TabBarController

- (id)init
{
    self = [super initWithNibName:NSStringFromClass([TabBarController class]) bundle:nil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self _initTabbarView];
    [self initViewControllers];
}

- (void)initAppData
{
    self.backgroudNormal = @[@"tabbar_1_n.png",
                             @"tabbar_2_n.png",
                             @"tabbar_3_n.png",
                             @"tabbar_4_n.png",
                             @"tabbar_5_n.png"];
    
    self.backgroudSelected = @[@"tabbar_1_s.png",
                               @"tabbar_2_s.png",
                               @"tabbar_3_s.png",
                               @"tabbar_4_s.png",
                               @"tabbar_5_s.png"];
}

- (void)initShopData
{
    self.backgroudNormal = @[@"shop_detail_btn_bg_n.png",
                             @"shop_photo_btn_bg_n.png",
                             @"shop_takephoto_btn_bg_n.png",
                             @"shop_talk_btn_bg_n.png",
                             @"shop_error_btn_bg_n.png"];
    
    self.backgroudSelected = @[@"shop_detail_btn_bg_s.png",
                             @"shop_photo_btn_bg_s.png",
                             @"shop_takephoto_btn_bg_s.png",
                             @"shop_talk_btn_bg_s.png",
                             @"shop_error_btn_bg_s.png"];
}

- (void)initViewControllers
{
    if (!_homeVC) {
        _homeVC = [[HomeViewController alloc] init];
        _homeNav = [[UINavigationController alloc] init];
        [_homeNav pushViewController:_homeVC animated:NO];
        _homeNav.delegate = self;
    }
    
    if (!_nearbyVC) {
        _nearbyVC = [[NearbyViewController alloc] init];
        _nearbyNav = [[UINavigationController alloc] init];
        [_nearbyNav pushViewController:_nearbyVC animated:NO];
        _nearbyNav.delegate = self;
    }
    
    if (!_photoVC) {
        _photoVC = [[PhotoViewController alloc] init];
        _photoNav = [[UINavigationController alloc] init];
        [_photoNav pushViewController:_photoVC animated:NO];
        _photoNav.delegate = self;
    }
    
    if (!_meVC) {
        _meVC = [[MeViewController alloc] init];
        _meNav = [[UINavigationController alloc] init];
        [_meNav pushViewController:_meVC animated:NO];
        _meNav.delegate = self;
    }
    
    if (!_moreVC) {
        _moreVC = [[MoreViewController alloc] init];
        _moreNav = [[UINavigationController alloc] init];
        [_moreNav pushViewController:_moreVC animated:NO];
        _moreNav.delegate = self;
    }
    
    if (!_loggedVC) {
        _loggedVC = [[LoggedViewController alloc] init];
        _loggedNav = [[UINavigationController alloc] init];
        [_loggedNav pushViewController:_loggedVC animated:NO];
        _loggedNav.delegate = self;
    }
    
//    if ([AppSetting isLogin]) {
//         self.viewControllers = @[_homeNav, _nearbyNav, _photoNav, _loggedNav, _moreNav];
//    } else {
//        self.viewControllers = @[_homeNav, _nearbyNav, _photoNav, _meNav, _moreNav];
//    }
    
    self.viewControllers = @[_homeNav, _nearbyNav, _photoNav, _meNav, _moreNav];
}

//创建自定义tabbar
- (void)_initTabbarView
{
    //隐藏tabbar工具栏
    [self.tabBar setHidden:YES];
    
    if (!_tabbarView) {
        _tabbarView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight-TabBarHeight, ScreenWidth, TabBarHeight)];
        [self.view addSubview:_tabbarView];
        
        self.selectedIndex = 0;
    }
    
    [self initButtons];
}

- (void)initButtons
{
    if(self.tab_delegate) {
        [self initShopData];
    }else {
        [self initAppData];
    }
        
    for (UIView *subview in _tabbarView.subviews) {
        [subview removeFromSuperview];
    }
    
    self.buttons = [NSMutableArray arrayWithCapacity:self.backgroudNormal.count];
    
    for (int i = 0; i < self.backgroudNormal.count; i++)
    {
        NSString *backImage = self.backgroudNormal[i];
        NSString *selectedImage = self.backgroudSelected[i];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:backImage] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
        
        float itemWidth = 320/self.backgroudNormal.count;
        button.frame = CGRectMake(itemWidth*i, 0, itemWidth, TabBarHeight);
        button.tag = i;
        
        if(self.tab_delegate) {
            [button addTarget:self.tab_delegate action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
        }else {
            [button addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        [_tabbarView addSubview:button];
        
        [self.buttons addObject:button];
    }
    
    if (self.selectedIndex != 2147483647) {
        [[self.buttons objectAtIndex:self.selectedIndex] setSelected:YES];
    }else {
        [[self.buttons objectAtIndex:0] setSelected:YES];
    }
}

- (void)selectedTab:(UIButton *)button {
    for (int i = 0; i < self.buttons.count; i++) {
        [[self.buttons objectAtIndex:i] setSelected:NO];
    }
    
    self.selectedIndex = button.tag;
    button.selected = YES;
    [self.delegate tabBarController:self didSelectViewController:nil];
}

- (void)selectedTabAtIndex:(int)buttonIndex
{
    UIButton *button = [self.buttons objectAtIndex:buttonIndex];

    [self selectedTab:button];
    
    for (int i = 0; i < self.buttons.count; i++) {
        [[self.buttons objectAtIndex:i] setSelected:NO];
    }
    button.selected = YES;
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

}
@end
